from selenium import webdriver
from time import sleep
from pyquery import PyQuery
import pymysql

# 京东数据的爬取
class jingdong:
    def __init__(self):
        self.chorme=webdriver.Chrome()
        self.chorme.get('https://www.jd.com/')
        self.db=pymysql.connect(host="localhost",user='root',password="xxxxxxxxxx",database='test',port=3306,charset='utf8')
        self.cursor=self.db.cursor()

    def searchResourse(self):
        self.Str1=input("输入要搜索的内容和页数，逗号分割: ")
        self.Str1List=self.Str1.split(',')
        self.chorme.find_elements_by_css_selector('#key')[0].send_keys(self.Str1List[0])
        self.chorme.find_elements_by_css_selector('#search > div > div.form > button')[0].click()
        for x in range(int(self.Str1List[1])):
            sleep(2)
            js='document.documentElement.scrollTop=100000'
            self.chorme.execute_script(js)
            sleep(3)
            self.chorme.execute_script(js)
            sleep(3)
            self.html=self.chorme.page_source
            self.contentAnalysis()
            self.pagingSearch()
    def pagingSearch(self):
        self.chorme.find_elements_by_css_selector('#J_bottomPage > span.p-num > a.pn-next')[0].click()
    def contentAnalysis(self):
        mypy=PyQuery(self.html)
        titleList=mypy('ul[class="gl-warp clearfix"] > li  div.p-name >a > em').items()
        priceList=mypy('ul[class="gl-warp clearfix"] > li  div.p-price > strong > i').items()
        for x in titleList:
            # print(x.text().replace('\n'," "))
            # print(next(priceList).text())
            # print('------------')
            # 执行sql语句
            self.cursor.execute('INSERT INTO jidongshuju(title,price) VALUES("{}","{}")'.format(x.text().replace('\n'," "),next(priceList).text()))
            # 提交数据
            self.db.commit()
    def mysqlClose(self):
        self.cursor.close()



if __name__ == '__main__':
    temp=jingdong()
    temp.searchResourse()
    temp.mysqlClose()